﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml;

namespace PrVyd.Zakladni_tridy
{
    class DBUtils
    {
        public static Person Element2Person(XElement element, bool isPayment)
        {
            Person person = new Person();
            person.Firm = element.Element("firm").Value;
            person.Ic = element.Element("ic").Value;
            person.Dic = element.Element("dic").Value;

            if (!isPayment)
            {
                person.City = element.Element("city").Value;
                person.Street = element.Element("street").Value;
                person.Zip = element.Element("zip").Value;
            }

            return person;
        }
        public static XElement Person2Element(Person person, XName elementName, bool isPayment)
        {
            XElement element = new XElement(elementName);
            element.Add(new XElement("firm", person.Firm));
            element.Add(new XElement("ic", person.Ic));
            element.Add(new XElement("dic", person.Dic));

            if (!isPayment)
            {
                element.Add(new XElement("city", person.City));
                element.Add(new XElement("street", person.Street));
                element.Add(new XElement("zip", person.Zip));
            }

            return element;

        }

        public static Item Element2Item(XElement element)
        {
            string prc = element.Element("price").Value;
            Item item = new Item();
            item.Description = element.Element("description").Value;
            item.Count = Int32.Parse(element.Element("count").Value);
            item.Price = string.IsNullOrEmpty(prc.Trim()) ? 0 : Decimal.Parse(prc);
            item.Tax = Decimal.Parse(element.Element("vat").Value);
            return item;
        }
        public static XElement Item2Element(Item item, String xName)
        {
            XElement element = new XElement(xName);

            element.Add(new XElement("name", item.Name));
            element.Add(new XElement("description", item.Description));
            element.Add(new XElement("count", item.Count.ToString()));
            element.Add(new XElement("price", item.Price));
            element.Add(new XElement("tax", item.Tax));

            return element;
        }

        public static Invoice Element2Invoice(XElement element)
        {
            Invoice invoice = new Invoice();
            invoice.Number = long.Parse(element.Attribute("number").Value);
            invoice.Due = DateTime.Parse(element.Elements("due").First().Value);
            invoice.Created = DateTime.Parse(element.Elements("created").First().Value);
            invoice.Customer = Element2Person(element.Elements("customer").First(), false);
            invoice.Supplier = Element2Person(element.Elements("supplier").First(), false);

            foreach (XElement xElem in element.Descendants("item"))
            {
                invoice.Items.Add(DBUtils.Element2Item(xElem));
            }

            

            return invoice;
        }
        public static XElement Invoice2Element(Invoice invoice)
        {
            XElement element = new XElement("invoice");
            element.Add(new XElement("number", invoice.Number));
            element.Add(new XElement("due", invoice.Due.ToString("yyyy-MM-dd")));
            element.Add(new XElement("created", invoice.Created.ToString("yyyy-MM-dd")));
            element.Add(Person2Element(invoice.Customer, "customer", false));
            element.Add(Person2Element(invoice.Supplier, "supplier", false));

            XElement items = new XElement("items");
            foreach (Item item in invoice.Items)
            {
                items.Add(DBUtils.Item2Element(item, "item"));
            }

            return element;

        }

        public static Payment Element2Payment(XElement element)
        {
            Payment payment = new Payment();
            payment.Number = Int32.Parse(element.Element("number").Value);
            payment.Amount = Decimal.Parse(element.Element("amount").Value.Replace('.',','));
            payment.VS = element.Element("variable_symbol").Value;
            payment.Date = DateTime.Parse(element.Element("date").Value);
            payment.Sender = DBUtils.Element2Person(element.Element("sender"), true);
            payment.Recipient = DBUtils.Element2Person(element.Element("recipient"), true);
            payment.Description = element.Element("description").Value;

            BankAccount accountOfSender = new BankAccount();
            accountOfSender.AccountNumber = element.Element("bank_account_of_sender").Value;
            accountOfSender.BankCode = element.Element("bank_code_of_sender").Value;
            if (element.Element("swift_of_sender") != null) accountOfSender.SWIFT = element.Element("swift_of_sender").Value;
            payment.BankAccountOfSender = accountOfSender;


            BankAccount accountOfRecipient = new BankAccount();
            accountOfRecipient.AccountNumber = element.Element("bank_account_of_recipient").Value;
            accountOfRecipient.BankCode = element.Element("bank_code_of_recipient").Value;
            if (element.Element("swift_of_sender") != null) accountOfRecipient.SWIFT = element.Element("swift_of_recipient").Value;
            payment.BankAccountOfRecipient = accountOfRecipient;



            return payment;
        }
        public static XElement Payment2Element(Payment payment)
        {
            XElement element = new XElement("payment");
            element.Add(new XElement("number", payment.Number));
            element.Add(new XElement("amount", payment.Amount.ToString()));
            element.Add(new XElement("variable_symbol", payment.VS));
            element.Add(new XElement("date", payment.Date.ToString("yyyy-MM-dd")));
            element.Add(DBUtils.Person2Element(payment.Sender, "sender", true));
            element.Add(DBUtils.Person2Element(payment.Recipient, "recipient", true));
            element.Add(new XElement("description", payment.Description));
            element.Add(new XElement("bank_account_of_sender", payment.BankAccountOfSender.AccountNumber));
            element.Add(new XElement("bank_code_of_sender", payment.BankAccountOfSender.BankCode));
            if (payment.BankAccountOfSender.SWIFT != null) element.Add(new XElement("swift_of_sender", payment.BankAccountOfSender.SWIFT));


            element.Add(new XElement("bank_account_of_recipient", payment.BankAccountOfRecipient.AccountNumber));
            element.Add(new XElement("bank_code_of_recipient", payment.BankAccountOfRecipient.BankCode));
            if (payment.BankAccountOfRecipient.SWIFT != null) element.Add(new XElement("swift_of_recipient", payment.BankAccountOfRecipient.SWIFT));


            return element;
        }

    }
}
